#include "njlikecandevice.h"

NJLikeCanDevice::NJLikeCanDevice( CanDevice* parent ) : CanDevice( parent ) {}

DWORD NJLikeCanDevice::CanDeviceOpen( DWORD dwType, DWORD dwIndex, CHAR* pDescription = nullptr ) {
    return CAN_DeviceOpen( dwType, dwIndex, pDescription );
}

DWORD NJLikeCanDevice::CanChannelStart( DWORD dwDeviceHandle, DWORD dwType, DWORD dwIndex, DWORD dwChannel, CanInitConfig* pInitConfig ) {
    Q_UNUSED( dwType )
    Q_UNUSED( dwIndex )

    CAN_InitConfig config;
    config.dwAccCode  = pInitConfig->dwAccCode;
    config.dwAccMask  = pInitConfig->dwAccMask;
    config.nFilter    = static_cast< BYTE >( pInitConfig->nFilter );
    config.bMode      = static_cast< BYTE >( pInitConfig->bMode );
    config.nBtrType   = static_cast< BYTE >( pInitConfig->nBtrType );
    config.dwBtr[ 0 ] = static_cast< BYTE >( pInitConfig->dwBtr[ 0 ] );
    config.dwBtr[ 1 ] = static_cast< BYTE >( pInitConfig->dwBtr[ 1 ] );

    return CAN_ChannelStart( dwDeviceHandle, dwChannel, &config );
}

ULONG NJLikeCanDevice::CanChannelSend( uint32 dwDeviceHandle, uint32 dwType, uint32 dwIndex, uint32 dwChannel, CanDataFrame* frame, uint64 nCount ) {
    Q_UNUSED( dwType )
    Q_UNUSED( dwIndex )
    CAN_DataFrame data[ 20 ];
    if ( nCount <= 20 ) {
        for ( uint64 i = 0; i < nCount; i++ ) {
            data[ i ].uID         = static_cast< UINT >( frame[ i ].uID );
            data[ i ].uTimeFlag   = static_cast< BYTE >( frame[ i ].uTimeFlag );
            data[ i ].nSendType   = static_cast< BYTE >( frame[ i ].nSendType );
            data[ i ].bRemoteFlag = static_cast< BYTE >( frame[ i ].bRemoteFlag );  //是否是远程帧
            data[ i ].bExternFlag = static_cast< BYTE >( frame[ i ].bExternFlag );  //是否是扩展帧
            data[ i ].nDataLen    = static_cast< BYTE >( frame[ i ].nDataLen );
            for ( int j = 0; j < data[ i ].nDataLen; j++ ) {
                data[ i ].arryData[ j ] = static_cast< BYTE >( frame[ i ].arryData[ j ] );
            }
        }
        return CAN_ChannelSend( dwDeviceHandle, dwChannel, data, nCount );
    }

    return 0;
}
ULONG NJLikeCanDevice::CanChannelReceive( uint32 dwDeviceHandle, uint32 dwType, uint32 dwIndex, uint32 dwChannel, CanDataFrame* pReceive, uint64 nCount, INT nWaitTime ) {
    Q_UNUSED( dwType )
    Q_UNUSED( dwIndex )
    CAN_DataFrame vco[ 200 ];
    ULONG         dwRel;
    dwRel = CAN_ChannelReceive( dwDeviceHandle, dwChannel, vco, nCount, nWaitTime );
    for ( ULONG i = 0; i < dwRel; i++ ) {
        pReceive[ i ].uID         = vco[ i ].uID;
        pReceive[ i ].uTimeFlag   = vco[ i ].uTimeFlag;
        pReceive[ i ].nSendType   = vco[ i ].nSendType;
        pReceive[ i ].bRemoteFlag = vco[ i ].bRemoteFlag;
        pReceive[ i ].bExternFlag = vco[ i ].bExternFlag;
        pReceive[ i ].nDataLen    = vco[ i ].nDataLen;
        for ( int j = 0; j < 8; j++ ) {
            pReceive[ i ].arryData[ j ] = vco[ i ].arryData[ j ];
        }
    }
    return dwRel;
}

DWORD NJLikeCanDevice::CanChannelStopDeviceClose( DWORD dwDeviceHandle, DWORD dwType, DWORD dwIndex, DWORD dwChannel ) {
    Q_UNUSED( dwType )
    Q_UNUSED( dwIndex )
    CanChannelStop( dwDeviceHandle, dwChannel );
    if ( CanDeviceClose( dwDeviceHandle ) == CAN_RESULT_OK ) { return true; }
    return false;
}

DWORD NJLikeCanDevice::CanDeviceClose( DWORD dwDeviceHandle ) {
    return CAN_DeviceClose( dwDeviceHandle );
}
DWORD NJLikeCanDevice::CanChannelStop( DWORD dwDeviceHandle, DWORD dwChannel ) {
    return CAN_ChannelStop( dwDeviceHandle, dwChannel );
}
